package com.haitaoit.chengquan.module.home.activity;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.utils.ToastUtils;

/**
 * Created by Administrator on 2017/5/6.
 */

public class CopyPopup {
    Activity activity;
     PopupWindow popupWindow;
    public CopyPopup(Activity activity){
        this.activity=activity;
    }

    private String text;
    public void show(View view,String text){
        this.text=text;
if(popupWindow==null)
    initPopupWindow();
        popupWindow.showAsDropDown(view,view.getWidth()/4,0);
//        popupWindow.showAsDropDown(view);
    }

    private void initPopupWindow() {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.popup_layout, null);
        // 设置按钮的点击事件
        TextView button = (TextView) contentView.findViewById(R.id.copy);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                ToastUtils.toastLong(activity, "看看");
                copy(text);
                popupWindow.dismiss();
            }
        });

popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

            popupWindow.setBackgroundDrawable(activity.getResources().getDrawable(
            R.drawable.popup_db));

//        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return false;
//                // 这里如果返回true的话，touch事件将被拦截
//                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
//            }
//        });
    }

private void copy(String text){
    ClipboardManager clipboard = (ClipboardManager)
            activity.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
}
}
